import common from '@/libs/planCommon'
import timeSlider from '@/components/time-slider/index'
import timeSlot from '@/components/time-slot/index'
import areasTem from '@/components/areas-tem/index'
import { ifError } from 'assert';

export default {
  name: 'baseMsg',
  mixins: [common],
  components:{
  	  timeSlider,
	  timeSlot,
	  areasTem
  },
  mounted(){
    this.checkAddSetp(2);
  	this.getAreasTem();
	this.getTimeTem();
	if(sessionStorage.getItem('campaign')){
		const campaign=JSON.parse(sessionStorage.getItem('campaign'));
		this.form.activity=campaign.activity;
		this.form.type=campaign.type+'';
		this.form.member=campaign.member;
		this.form.day_budget=campaign.day_budget;
		this.form.speed_type=campaign.speed_type+'';
		this.areasTemSel=campaign.areasTemSel;
		this.timeTemSel=campaign.timeTemSel;
		this.timeSolt=campaign.start_time.substring(0,10)+'&'+campaign.end_time.substring(0,10);
	}
	this.getActivity();
  },
  data () {
  	 return {
  	 	timeType:'tem',
  	 	areaType:'tem',
  	 	form:{
			activity:'',
  	 		type:'2',
  	 		day_budget:'',
  	 		speed_type:'1',
  	 		area_id_list:[],
  	 		workday:[],
  	 		weekend:[],
  	 		start_time:'',
			end_time:'',
			member:''
  	 	},
  	 	areasTem:null,
  	 	timeTem:null,
  	 	areasChecked_notcommon:[],
  	 	areaTemNameID:'',
  	 	areasTemSel:[],
  	 	timeTemSel:[],
		timeTemOptions:{},
		areasOptions:{},  
		timeSolt:'',
		planModelList:[
			{
				value:'计划模式一',
				label:'计划模式一'
			},
			{
				value:'计划模式二',
				label:'计划模式二'
			}
		],
		priceMOdelList:[
			{
				value:'2',
				label:'CPM'
			},
			{
				value:'8',
				label:'CPC'
			}
		],
		activityTypeList:[
			{
				value:'活动一',
				label:'活动一'
			}
		]
  	 }
  },
  watch:{
	areaType(val){
		if(val=='cus'){
			this.areasOptions={
				 id:'',
				 name:'',
				 list:[]
			 };
		}
	},
	timeType(val){
		if(val=='my'){
			this.timeTemOptions={
				name:'',
				id:'',
				workday:'',
				weekend:''
			}
	   }
	}  
  },
  methods:{
	getActivity(){
		this.activityTypeList=[];
		this.$http({
			methods:'get',
			url:"/zuanzhan/campaign/activity",
		}).then(data=>{
			data.data.data.forEach(elemet=>{
				   this.activityTypeList.push({
						value:elemet,
						label:elemet
				   });
			})
			
		}).catch(error=>{

		});
	},
  	getAreasTem(){
  		  this.areasTem=null;
  			this.$http.get('/zuanzhan/area/template-list').then((data)=>{
  		  	this.$http.interceptors(this,data);
  		    this.areasTem=data.data.data;
  		 }).catch(function (error) {
		
		 });
  	},
  	getTimeTem(){
  		  this.timeTem=null;
  			this.$http.get('/zuanzhan/put-times').then((data)=>{
  		  	this.$http.interceptors(this,data);
  		    this.timeTem=data.data.data;
  		 }).catch(function (error) {
		 });
  	},
  	timeTemChange(val)
  	{
  		  this.timeTemOptions={};
  		  this.timeType='tem';
  		  this.getTimeTem();
	},
	areasTemChange(){
		 this.areasOptions={};
		 this.areaType="tem";
		 this.getAreasTem();
	},
  	areasTemClick(ev){
  		let target=ev.target.dataset;
  		 switch(target.type)
  		 {
  		 	  case 'delete':
  		 	  	this.$http.get('/zuanzhan/area/template-del',{
					    params: {
					      id: target.name
					    }
					  }).then((data)=>{
			  		  	this.$http.interceptors(this,data);
			  		  	this.n3Toast({
					        text: '删除成功',
					        type: 'success',
					        placement: 'top',
					        closeOnClick : true
					      });
					      this.getAreasTem();
			  		 }).catch(function (error) {
					
					});
  		 	  break;
  		 	  case 'update':
  		 	       this.areaType='update',this.areaTemName=target.name,this.areaTemNameID=target.id,this.orSaveTem=true;
  		 	       for(let i=0,o;o=this.areasTem[i++];)
  		 	       {
  		 	       	    if(o.id==this.areaTemNameID)
  		 	       	    {
  		 	       	    	   this.areasChecked=o.area_id_list;
  		 	       	    }
				   }
				    this.areasOptions={
				 	   id:this.areaTemNameID,
				  	   name:this.areaTemName,
				  	   list:this.areasChecked
					 };
  		 	  break
  		 }
  	},
  	timeTemClick(ev){
  		let target=ev.target.dataset;
  		 switch(target.type)
  		 {
  		 	  case 'delete':
  		 	  	this.$http.get('/zuanzhan/put-time/del',{
					    params: {
					      id: target.id
					    }
					  }).then((data)=>{
			  		  	this.$http.interceptors(this,data);
			  		  	this.n3Toast({
					        text: '删除成功',
					        type: 'success',
					        placement: 'top',
					        closeOnClick : true
					      });
					      this.getTimeTem();
			  		 }).catch(function (error) {
						    console.log(error);
						  });
  		 	  break;
  		 	  case 'update':
  		 	       this.timeType='update';
  		 	       let id=target.id;
  		 	       for(var i=0,o;o=this.timeTem[i++];)
  		 	       {
  		 	       	   if(o.id==id){
  		 	       	   		this.timeTemOptions={
  		 	       	   				name:o.name,
									id:o.id,
									workday:o.workday,
									weekend:o.weekend
  		 	       	   		}
  		 	       	   }
  		 	       }
  		 	       
  		 	  break
  		 }
		},
		toPage(){
			console.log(this.areasTemSel);
			console.log(this.timeTemSel);
			this.form.area_id_list=[];
			this.form.workday=[];
			this.form.weekend=[];
			this.form.start_time='';
			this.form.end_time='';
			for(let i=0,o;o=this.areasTem[i++];){
				 if(o.id==this.areasTemSel){
				 		this.form.area_id_list=o.area_id_list;
				 }
			}
			for(let i=0,o;o=this.timeTem[i++];)
			{
				 if(o.id==this.timeTemSel)
				 {
				 	this.form.workday=o.workday;
  	 				this.form.weekend=o.weekend;
				 }
			}
            if(this.form.activity==''){
				this.$message.error('请填写活动类型');
				return false;
			}
			if(this.areasTemSel=='')
			{
				this.$message.error('请选择地域模板');
				return false;
			}
			if(this.timeTemSel=='')
			{
				this.$message.error('请选择时间模板');
				return false;
			}
			if(this.form.day_budget==''|| this.form.day_budget<300 ||this.form.day_budget>3083.44 )
			{
				this.$message.error('请按规则填写每日预算');
				return false;
			}
			this.form.start_time=this.timeSolt.split('&')[0]+' 00:00:00';
			this.form.end_time=this.timeSolt.split('&')[1]+' 00:00:00';
			let json=Object.assign({},this.form);
			json.day_budget=parseInt(json.day_budget);
			json.speed_type=parseInt(json.speed_type);
			json.type=parseInt(json.type);
			json.areasTemSel=this.areasTemSel;
			json.timeTemSel=this.timeTemSel;
			sessionStorage.setItem('campaign',JSON.stringify(json));
			// this.$store.commit('setaddPlanMaxStep',0);
			  // this.$store.commit('setaddPlanNowStep',2);
			this.$store.commit('setPlanStepPool','unit');
			this.$parent.nowStep='unit';
			this.$router.push('/add_plan/setunit');	
		}
  }
}


























